package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.third.faceid.v2.FaceIdOcrV3Record;
import com.ucredit.babel.common.model.third.faceid.v2.FaceV2Data;
import com.ucredit.babel.common.model.third.faceid.v2.FaceV2Item;
import com.ucredit.babel.common.model.third.faceid.v2.FaceV2Legality;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;

/**
 * FACE++身份证识别
 *
 * @author xj
 */

public class FaceidOCRParse extends ParseBase {
    private Text key = new Text();
    private Text val = new Text();

    /**
     * FACE++身份证识别
     *
     * @param model
     * @return
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        FaceIdOcrV3Record obj = (FaceIdOcrV3Record) model;
        FaceV2Data data = obj.getData();
        FaceV2Item birthYearItem = data.getBirthYear();
        FaceV2Item nameItem = data.getName();
        FaceV2Item genderItem = data.getGender();
        FaceV2Item nationalityItem = data.getNationality();
        FaceV2Item birthMonthItem = data.getBirthMonth();
        FaceV2Item birthDayItem = data.getBirthDay();
        FaceV2Item idCardItem = data.getIdCardNumber();
        FaceV2Item addressItem = data.getBirthMonth();
        FaceV2Item portraitItem = data.getPortrait();
        FaceV2Item issuedByItem = data.getIssuedBy();
        FaceV2Item validDataStartItem = data.getValidDateStart();
        FaceV2Item validDataEndItem = data.getValidDateEnd();
        FaceV2Legality legalityItem = data.getLegality();

        StringBuilder sb = getBaseInfo(obj, successful);

        //请求ID
        sb.append(data.getRequestId() == null ? "" : data.getRequestId()).append(CRT001);
        //识别结果
        sb.append(data.getResult() == null ? "" : data.getResult()).append(CRT001);
        //正反面
        sb.append(data.getSide()).append(CRT001);
        //姓名
        appendItemToSb(sb, nameItem);
        //性别
        appendItemToSb(sb, genderItem);
        //民族
        appendItemToSb(sb, nationalityItem);
        //出生年份
        appendItemToSb(sb, birthYearItem);
        //出生月份
        appendItemToSb(sb, birthMonthItem);
        //出生日
        appendItemToSb(sb, birthDayItem);
        //身份证号
        appendItemToSb(sb, idCardItem);
        //地址
        appendItemToSb(sb, addressItem);
        //头像图片
        appendItemToSb(sb, portraitItem);
        //签发机关
        appendItemToSb(sb, issuedByItem);
        //有效日期起始时间
        appendItemToSb(sb, validDataStartItem);
        //有效日期终止时间
        appendItemToSb(sb, validDataEndItem);
        //合法性检查结果_真实身份证照片
        sb.append(legalityItem == null ? "" : legalityItem.getIdPhoto() == null ? "" : legalityItem.getIdPhoto()).append(CRT001);
        //合法性检查结果_临时身份证照片
        sb.append(legalityItem == null ? "" : legalityItem.getTemporaryIdPhoto() == null ? "" : legalityItem.getTemporaryIdPhoto()).append(CRT001);
        //合法性检查结果_身份证的复印件
        sb.append(legalityItem == null ? "" : legalityItem.getPhotoCopy() == null ? "" : legalityItem.getPhotoCopy()).append(CRT001);
        //合法性检查结果_屏幕翻拍的照片
        sb.append(legalityItem == null ? "" : legalityItem.getScreen() == null ? "" : legalityItem.getScreen()).append(CRT001);
        //合法性检查结果_合成或者编辑过的图片
        sb.append(legalityItem == null ? "" : legalityItem.getEdited() == null ? "" : legalityItem.getEdited()).append(CRT001);
        //合法性检查结果_真实身份证照片的阈值
        sb.append(legalityItem == null ? "" : legalityItem.getIdPhotoThreshold() == null ? "" : legalityItem.getIdPhotoThreshold()).append(CRT001);
        //完整性
        sb.append(data.getCompleteness()).append(CRT001);
        //图片坐标
        sb.append(data.getCardRect() == null ? "" : JSON.toJSONString(data.getCardRect())).append(CRT001);
        //错误码
        sb.append(data.getError() == null ? "" : data.getError()).append(CRT001);
        key.set(getKey());
        val.set(sb.toString().replace("\n", " ").replace("\r", " "));
        context.write(key, val);

        return true;
    }

    private void appendItemToSb(StringBuilder sb, FaceV2Item item) {
        //识别内容
        sb.append(item == null ? "" : item.getResult() == null ? "" : item.getResult()).append(CRT001);
        //质量问题
        sb.append(item == null ? "" : item.getQuality()).append(CRT001);
        //四点坐标
        sb.append(item == null ? "" : item.getRect() == null ? "" : JSON.toJSONString(item.getRect())).append(CRT001);
        //逻辑问题
        sb.append(item == null ? "" : item.getLogic() == null).append(CRT001);
    }

    @Override
    public String getTableName() {
        return "kafka_babel_faceid_ocr_inc";
    }

    @Override
    public String getType() {
        return RecordType.FACEID_V2_LOG.toString();
    }

    @Override
    public String getComment() {
        return "FACE++身份证识别";
    }
}
